package internationalenergytradingplatform.service.Impl;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import internationalenergytradingplatform.constant.MessageConstant;
import internationalenergytradingplatform.dao.BuyerAccountDao;
import internationalenergytradingplatform.dao.CommodityDao;
import internationalenergytradingplatform.dao.OrderMapper;
import internationalenergytradingplatform.dao.SupplierAccountDao;
import internationalenergytradingplatform.dto.DistanceDTO;
import internationalenergytradingplatform.exception.AccountNotFoundException;
import internationalenergytradingplatform.pojo.BuyerAccount;
import internationalenergytradingplatform.pojo.Commodity;
import internationalenergytradingplatform.pojo.Order;
import internationalenergytradingplatform.pojo.SupplierAccount;
import internationalenergytradingplatform.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    BuyerAccountDao buyerAccountDao;

    @Autowired
    OrderMapper orderMapper;

    @Autowired
    SupplierAccountDao supplierAccountDao;

    @Autowired
    CommodityDao commodityDao;

    @Override
    public Double calculateShippingCosts(Integer commodityId, String buyer) {
        //获取采购商地址
        BuyerAccount buyerByTele = buyerAccountDao.getBuyerByTele(buyer);
        String buyerAddress = buyerByTele.getAddress();
        //获取供应商地址
        Commodity commodityById = commodityDao.getCommodityById(commodityId);
        String supplierTele = commodityById.getSupplierAccount();
        SupplierAccount supplier = supplierAccountDao.getCompanyByTele(supplierTele);
        String supplierAddress = supplier.getAddress();
        //创建地址DTO
        DistanceDTO distanceDTO = new DistanceDTO();
        //判断双方地址是否在距离库中
        if (buyerAddress.contains("沈阳")) {
            if (supplierAddress.contains("克拉玛依")) {
                distanceDTO.setDistance(3108614);
                distanceDTO.setDeparture_convenience(4);
                distanceDTO.setDestination_convenience(1);
            } else if (supplierAddress.contains("北京")) {
                distanceDTO.setDistance(631389);
                distanceDTO.setDeparture_convenience(4);
                distanceDTO.setDestination_convenience(5);
            }
        } else if (buyerAddress.contains("郑州")) {
            if (supplierAddress.contains("克拉玛依")) {
                distanceDTO.setDistance(2711509);
                distanceDTO.setDeparture_convenience(4);
                distanceDTO.setDestination_convenience(1);
            } else if (supplierAddress.contains("北京")) {
                distanceDTO.setDistance(613497);
                distanceDTO.setDeparture_convenience(4);
                distanceDTO.setDestination_convenience(5);
            }
        } else {
            throw new AccountNotFoundException("双方地址信息不在库中，运费无法计算！");
        }
        //向机器学习接口发起请求
        String url = "http://39.98.200.99:5000/predict";
        String response = HttpUtil.post(url, JSONUtil.toJsonStr(distanceDTO));
        // 解析JSON字符串
        JSONObject jsonResponse = JSONUtil.parseObj(response);
        // 提取double类型的值
        Double predicted_fee = jsonResponse.getDouble("predicted_fee");
        predicted_fee = Math.ceil(predicted_fee);//向上取整
        predicted_fee = Math.abs(predicted_fee);
        return predicted_fee;
    }
}
